{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adding a category to capture NA\n",
    "\n",
    "This is perhaps the most widely used method of missing data imputation for categorical variables. This method consists in treating missing data as if they were an additional label or category of the variable. All the missing observations are grouped in the newly created label 'Missing'. \n",
    "\n",
    "The beauty of this technique resides on the fact that it does not assume anything on the missingness of the values. It is very well suited when the number of missing data is high.\n",
    "\n",
    "\n",
    "### Advantages\n",
    "\n",
    "- Easy to implement\n",
    "- Captures the importance of missingess if there is one\n",
    "\n",
    "### Disadvantages\n",
    "\n",
    "- If the number of NA is small, creating an additional category may cause trees to over-fit\n",
    "\n",
    "I would say that for categorical variables this is the method of choice, as it treats missing values as a separate category, without making any assumption on their missingness. It is used widely in data science competitions and business settings. See for example the winning solution of the KDD 2009 cup: \"Winning the KDD Cup Orange Challenge with Ensemble Selection\" (http://www.mtome.com/Publications/CiML/CiML-v3-book.pdf).\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "===============================================================================\n",
    "\n",
    "## Real Life example: \n",
    "\n",
    "### Predicting Sale Price of Houses\n",
    "\n",
    "The problem at hand aims to predict the final sale price of homes based on different explanatory variables describing aspects of residential homes. Predicting house prices is useful to identify fruitful investments, or to determine whether the price advertised for a house is over or underestimated, before making a buying judgment.\n",
    "\n",
    "=============================================================================\n",
    "\n",
    "In the following cells, I will demonstrate NA imputation by adding an additional label using the House Price datasets from Kaggle.\n",
    "\n",
    "If you haven't downloaded the datasets yet, in the lecture \"Guide to setting up your computer\" in section 1, you can find the details on how to do so."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "% matplotlib inline\n",
    "\n",
    "# for regression problems\n",
    "from sklearn.linear_model import LinearRegression, Ridge\n",
    "\n",
    "# to split and standarize the datasets\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "# to evaluate regression models\n",
    "from sklearn.metrics import mean_squared_error\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### House Price dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SalePrice      0.000000\n",
       "BsmtQual       0.025342\n",
       "GarageType     0.055479\n",
       "FireplaceQu    0.472603\n",
       "dtype: float64"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# let's load the dataset with a few columns for the demonstration\n",
    "cols_to_use = ['BsmtQual', 'FireplaceQu', 'GarageType', 'SalePrice']\n",
    "\n",
    "data = pd.read_csv('houseprice.csv', usecols=cols_to_use)\n",
    "\n",
    "# let's inspect the percentage of missing values in each variable\n",
    "data.isnull().mean().sort_values(ascending=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((1022, 3), (438, 3))"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# let's separate into training and testing set\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(data[['BsmtQual', 'FireplaceQu', 'GarageType']],\n",
    "                                                    data.SalePrice, test_size=0.3,\n",
    "                                                    random_state=0)\n",
    "X_train.shape, X_test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# let's create a variable to replace NA with the most frequent label or a random sample\n",
    "\n",
    "def impute_na(df_train, df_test, variable):\n",
    "    df_train[variable+'_NA'] = np.where(df_train[variable].isnull(), 'Missing', df_train[variable])\n",
    "    df_test[variable+'_NA'] = np.where(df_test[variable].isnull(), 'Missing', df_test[variable])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# and let's replace the NA\n",
    "for variable in ['BsmtQual', 'FireplaceQu', 'GarageType',]:\n",
    "    impute_na(X_train, X_test, variable)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "BsmtQual           24\n",
       "FireplaceQu       478\n",
       "GarageType         54\n",
       "BsmtQual_NA         0\n",
       "FireplaceQu_NA      0\n",
       "GarageType_NA       0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# let's check that data have been completed\n",
    "X_train.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>BsmtQual</th>\n",
       "      <th>FireplaceQu</th>\n",
       "      <th>GarageType</th>\n",
       "      <th>BsmtQual_NA</th>\n",
       "      <th>FireplaceQu_NA</th>\n",
       "      <th>GarageType_NA</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>64</th>\n",
       "      <td>Gd</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Attchd</td>\n",
       "      <td>Gd</td>\n",
       "      <td>Missing</td>\n",
       "      <td>Attchd</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>682</th>\n",
       "      <td>Gd</td>\n",
       "      <td>Gd</td>\n",
       "      <td>Attchd</td>\n",
       "      <td>Gd</td>\n",
       "      <td>Gd</td>\n",
       "      <td>Attchd</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>960</th>\n",
       "      <td>TA</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>TA</td>\n",
       "      <td>Missing</td>\n",
       "      <td>Missing</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1384</th>\n",
       "      <td>TA</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Detchd</td>\n",
       "      <td>TA</td>\n",
       "      <td>Missing</td>\n",
       "      <td>Detchd</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1100</th>\n",
       "      <td>TA</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Detchd</td>\n",
       "      <td>TA</td>\n",
       "      <td>Missing</td>\n",
       "      <td>Detchd</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     BsmtQual FireplaceQu GarageType BsmtQual_NA FireplaceQu_NA GarageType_NA\n",
       "64         Gd         NaN     Attchd          Gd        Missing        Attchd\n",
       "682        Gd          Gd     Attchd          Gd             Gd        Attchd\n",
       "960        TA         NaN        NaN          TA        Missing       Missing\n",
       "1384       TA         NaN     Detchd          TA        Missing        Detchd\n",
       "1100       TA         NaN     Detchd          TA        Missing        Detchd"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# let's see how the new variable looks like, where data was missing we have\n",
    "# not the label 'Missing'\n",
    "\n",
    "X_train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAEmCAYAAABs7FscAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAEv1JREFUeJzt3X+wZ3Vdx/Hnq+WXk6Ewe6MddvNiLdniCOKGP7IiyYGk\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AwwSU4GHl9VJ1bVZFVNMjgB7UlZzZtBr6Vufx/9rcA7\ngU1VtQ84E7gtyS0Mujr+YITvvB5Yt/9kbNft8wvAe5N8Evgk8NKququb/3XAS7t1rRz6niuBTUlu\nA57C1HNhRrUZWNMNX8hXPh3yGgx6HQKfdSNNk+QK4JnA2d3JW2lZM+glqXFedSMtkiRnA789rfme\nqnpRH/VofLlHL0mN82SsJDXOoJekxhn0ktQ4g16SGvd/wffx/8ADWecAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x8cb93f8710>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAEmCAYAAABs7FscAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAFWlJREFUeJzt3X+0XWV95/H3pwGBpYNCiWkWCU3axrZBBWpEbWlrpRZa\nrMGqGJw66ZQxjqLizLQMdGZWf610UWdNV6uV1ihqxBFMi0iEGSiNUH+MJYSfEjAlQ6DABBKlU1t0\nUOJ3/jg7crjk5p6be+49uc99v9bKOns/+9lnf/cSP+e5z95nn1QVkqR2fd+oC5AkTS+DXpIaZ9BL\nUuMMeklqnEEvSY0z6CWpcQa9JDVuoKBPcn+SryS5PcmWru3oJNcnubd7Paqv/4VJtifZluS06Spe\nkjSxyYzof66qTqyqFd36BcCmqloGbOrWSbIcWAUcD5wOXJxk3hBrliRNwiFT2Hcl8MpueT1wI/Af\nu/bLq+oJYEeS7cDJwJfHe6NjjjmmlixZMoVSJGnuueWWW75WVfMn6jdo0Bfw10n2AB+sqnXAgqra\n2W1/BFjQLR8L/G3fvg91bU+TZA2wBuC4445jy5YtA5YiSQJI8sAg/QYN+lOq6uEkzweuT/LV/o1V\nVUkm9dCc7sNiHcCKFSt84I4kTZOB5uir6uHudRdwJb2pmEeTLAToXnd13R8GFvftvqhrkySNwIRB\nn+TZSf7F3mXgF4C7gI3A6q7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/m79+4FXAdcP+J6aw5yj12x1HfD2\nJIcCJHlBkmd3205OsrQL1zcBXxyz73OBf+hC/sfoPdUU4G+Bn0mytHvPvVM31wHvSpKu/aSu/QPA\nryU5sWv/fmAt8Pvd9vvpBT/Aa4FDBzmxqvoqcDfwy937Hgn8NHBcVS2pqiX0rg+cPcj7SQa9ZqsP\n0wvDW5PcBXyQp/5CvRn4U3q/F7yDZz6p9FrgkCT3ABfRC3iqajewBvh0kjuAT3X9f59eSN+ZZGu3\nTlXtBH4VWJdkG/B/gPdV1d6R+IeAn+3e6xU8/S+SiawFFnXLr6N3jeCJvu1XAb/cXZ+Q9stn3agp\n3bTMb1TVa0Zw7HcAb6d3MfcfZvr40ngc0UtDUlUXV9WLDHkdbBzRSzMoyYuAS8c0P1FVLxtFPZob\nDHpJapxTN5LUOINekhpn0EtS4wx6SWrc/weJruW0ibHWyAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x8cb949a470>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAEtCAYAAAAGK6vfAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAHsRJREFUeJzt3Xu4XFV9//H3B6KIFxAeTiMSMGiDNngBG1OsPlZBBasV\nvGHwFhWNrXipl18btP3Zqqm0/VVrtaioaFSEBhWIaBEIUqtVQ0AUAiKpgYYIJN5RFAp8fn/sNTA5\nnJyZOZfsmcXn9Tx5zszae2a+k5x8Zs/aa68l20RERL12aruAiIiYXQn6iIjKJegjIiqXoI+IqFyC\nPiKicgn6iIjKJegjIiqXoI+IqFyCPiKicnPaLgBgr7328vz589suIyJipFx88cU/tj3Wa7+hCPr5\n8+ezbt26tsuIiBgpkq7tZ7903UREVC5BHxFRuQR9RETlEvQREZVL0EdEVC5BHxFRuQR9RETlEvQR\nEZXrK+glPVDS5yR9X9KVkh4vaU9J50m6uvz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kEfw2A98LzOHdvHlal11wFvoum7fgxNV+Nvux5350pcZX3Rp9IsLn2z\npAvpWhR7O3YCDrH92x77deq6RtLPysISBwPndm8evzvN1aivtb2mn+fvsgL4LNsu/XcMcIika8r9\nMeCPgK8O+NxxD5Q++hgGFwD36R5xAty3/NwduL4cub4U2Hk7z7E78LMS8o+gWS8V4CLgjyTtUU5y\nPq/rMecCr+/c6V6vdBIfB06hWX2q+2j6BWocQPNt4mqarqDXdk6uSnp46VaaVFmI/L+BTl//A8v7\nmWd7vu35wBtowj+ipwR9tK4siHEUTSBvlLQWWAn8JXAisFTN2qePYOL1dAHOAeZIupKmn7sza+Nm\n4O9o+rO/AVwD/KI85g3AonKi9ArgT/so9wyaD5VPjmvfTPMN5IvAMtu3Ah+hCfxLy7zwH6L/b9Hv\npvnAgObD6Tzb/9u1/UzgKEmZuyZ6ylw3Ub1Ov3s5sj6DZqHvM6b4XIcA77H9lK62zwCfs33mzFQc\nMbNyRB/3BH9TTnBeDmykORoemKS3A/9Gs7BHxMjIEX3EDlTOA3xyXPPNtv+whXLiHiJBHxFRuXTd\nRERULkEfEVG5BH1EROUS9BERlUvQR0RU7v8Dz2thssqBmaAAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x8cba8b32e8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# let's look at the number of observations per label on each of the variables\n",
    "for col in ['BsmtQual_NA', 'FireplaceQu_NA', 'GarageType_NA']:\n",
    "    X_train.groupby([col])[col].count().sort_values(ascending=False).plot.bar()\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We observe in the plots the label indicating the number of observations with missing values, within each of the 3 categorical variables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# let's transform the categories into numbers quick and dirty so we can use them in scikit-learn\n",
    "\n",
    "# the below function numbers the labels from 0 to n, n being the number of different labels \n",
    "#  within the variable\n",
    "\n",
    "for col in ['BsmtQual_NA', 'FireplaceQu_NA', 'GarageType_NA',]:\n",
    "    labels_dict = {k:i for i, k in enumerate(X_train[col].unique(), 0)}\n",
    "    X_train.loc[:, col] = X_train.loc[:, col].map(labels_dict )\n",
    "    X_test.loc[:, col] = X_test.loc[:, col].map(labels_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Linear Regression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train set random imputation\n",
      "Linear Regression mse: 4810016310.466396\n",
      "Test set random imputation\n",
      "Linear Regression mse: 5562566516.826057\n"
     ]
    }
   ],
   "source": [
    "# Let's evaluate the performance of Linear Regression\n",
    "\n",
    "linreg = LinearRegression()\n",
    "linreg.fit(X_train[['BsmtQual_NA', 'FireplaceQu_NA', 'GarageType_NA',]], y_train)\n",
    "print('Train set random imputation')\n",
    "pred = linreg.predict(X_train[['BsmtQual_NA', 'FireplaceQu_NA', 'GarageType_NA',]])\n",
    "print('Linear Regression mse: {}'.format(mean_squared_error(y_train, pred)))\n",
    "print('Test set random imputation')\n",
    "pred = linreg.predict(X_test[['BsmtQual_NA', 'FireplaceQu_NA', 'GarageType_NA',]])\n",
    "print('Linear Regression mse: {}'.format(mean_squared_error(y_test, pred)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the previous lectures we trained linear regressions on data where missing observations were replaced by i) random sampling, or ii) random sampling plus a variable to indicate missingness. We obtained the following mse for the testing sets:\n",
    "\n",
    "- frequent label imputation mse: 6456070592\n",
    "- random sampling + additional category: 4911877327\n",
    "- adding 'missing' label: 5562566516\n",
    "\n",
    "Therefore, adding an additional 'Missing' category lies between the 2 other approaches in terms of performance.\n",
    "\n",
    "A next step could be to investigate which approach works better for each variable individually, to try and optimise the performance of logistic regression even more."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "**That is all for this demonstration. I hope you enjoyed the notebook, and see you in the next one.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
